<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData">
			<uni-forms-item name="test_paper_id" label="试卷昵称">
			<!-- 	<uni-data-select v-model="formData.test_paper_id" :localdata="localData"  @change="change">
				</uni-data-select> -->
				
				<cu-select :localdata="localData" v-model="formData.test_paper_id" @onSearch="fillterMech"  @change="change">
				</cu-select>
				
				
			</uni-forms-item>
			<uni-forms-item name="radio" label="单选数量">
				<uni-easyinput type="number" v-model="formData.radio"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="multiple" label="多选数量">
				<uni-easyinput type="number" v-model="formData.multiple"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="judge" label="判断数量">
				<uni-easyinput type="number" v-model="formData.judge"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<button class="uni-button" style="width: 100px;margin-left: 15px;" @click="$emit('close')">关闭</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/role-item.js';
	import ceSelect from '@/pages/mechanism/component/cuSelect.vue'

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'role-item';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		name: 'add-role-item',
		props: {
			sid: ""
		},
		emits:['refreshData'],
		components: {
			'cu-select': ceSelect
		},
		data() {
			let formData = {
				"s_id": "",
				"test_paper_id": "",
				"radio": 0,
				"multiple": 0,
				"judge": 0
			}
			return {
				localData:[],
				copyList:[],
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		watch:{
			"formData.test_paper_id"(nv){
				console.log(nv)
			}
		},
		created() {
			
		},
		mounted() {
			this.formData.s_id = this.sid
			this.getAllList()
		},
		methods: {
			fillterMech(e){
				console.log('e',e)
				if(e==''){
					this.localData = this.copyList
					return 
				}
				let searchData = [] 
				for (let row of this.copyList) {
					if(row.text.indexOf(e)!=-1){
						searchData.push(row)
					}
				}
				if(searchData.length>0){
					this.localData = searchData
				}
			},
			async getAllList() {
				let db = uniCloud.database()
				const responseTotal = await db.collection('test-paper').count()
				const size = responseTotal.result.total
				console.log('all',size)
				if (size > 0) {
					let total = size
					let processSize = 0
					let position = 1
					let allData = []
					while (processSize < total) {
						const responseData = await db.collection('test-paper')
							.field('s_id as value,name as text,s_id')
							.skip(processSize).limit(500).get()
						if (responseData.result.data.length > 0) {
							responseData.result.data.forEach((e) => {
								e.indexId = position++
								allData.push(e)
							})
						}
						processSize += responseData.result.data.length
						console.log(processSize,responseData.result.data.length)
					}
					
					for (let s of allData) {
						s.text = s.text +'('+s.s_id+')'
					}
					console.log(allData)
					
					this.copyList = allData
					let temp =  [] 
					for (let s of this.copyList) {
						temp.push(s)
					}
					
					
					this.localData = temp
				}
			},
			
			async getLocalTestPaper(){
				const resultData = await db.collection('test-paper').field({
					_id:true,
					name:true,
					s_id:true,
				}).limit(500).get()
				console.log(resultData)
				for (let s of resultData.result.data) {
					this.localData.push({
						value:s.s_id,
						text:`${s.name}(${s.s_id})`
					})
				}
				console.log(this.localData)
			},
			change(e){
				console.log(e)
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					console.log(res)
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				const self = this 
				value.s_id = this.sid
				value.judge = parseInt(value.judge)
				value.radio = parseInt(value.radio)
				value.multiple = parseInt(value.multiple)
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					self.$emit('refreshData')
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>
<style>
	.uni-container {
		background-color: #fff;
	}
</style>
